//package com.zmn.plat.dubbo.impl.company;
//
//import com.zmn.common.constant.GlobalConsts;
//import com.zmn.common.dto.VtDTO;
//import com.zmn.common.dto2.ResponseDTO;
//import com.zmn.common.utils.number.NumberUtil;
//import com.zmn.gateway.dubbo.client.GUserHolder;
//import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
//import com.zmn.plat.business.interfaces.company.BaseCompanyAreaBService;
//import com.zmn.plat.business.interfaces.company.BaseCompanyBService;
//import com.zmn.plat.common.dto.company.*;
//import com.zmn.plat.common.exception.PlatException;
//import com.zmn.plat.dubbo.interfaces.company.BaseCompanyListRemoteService;
//import com.zmn.plat.dubbo.utils.DubboConsts;
//import com.zmn.plat.manager.interfaces.cache.CompanyCache;
//import com.zmn.plat.model.entity.company.BaseCompany;
//import com.zmn.plat.model.entity.company.BaseCompanyQuery;
//import com.zmn.plat.model.entity.company.BaseCompanyTarget;
//import com.zmn.plat.model.entity.company.CompanyTypeQuery;
//import com.zmn.plat.model.vo.company.BaseCompanyVO;
//import com.zmn.plat.persistence.interfaces.company.CompanyDao;
//import com.zmn.plat.services.interfaces.company.BaseCompanyService;
//import com.zmn.plat.utils.bean.BeanCopyUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.dubbo.config.annotation.Service;
//import org.jetbrains.annotations.NotNull;
//import org.springframework.beans.BeanUtils;
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.Cacheable;
//import org.springframework.util.CollectionUtils;
//
//import javax.annotation.Resource;
//import java.util.*;
//import java.util.stream.Collectors;
//
///**
// * @author duanzuocai
// * @version v1.0
// * @since 2018/10/18 9:22
// */
//@Slf4j
//@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
//@CacheConfig(cacheNames = "redis1h")
//public class BaseCompanyListRemoteServiceImpl implements BaseCompanyListRemoteService {
//    @Resource
//    CompanyCache companyCache;
//    @Resource
//    CompanyDao companyDao;
//    @Resource
//    BaseCompanyService baseCompanyService;
//    @Resource
//    BaseCompanyBService baseCompanyBService;
//    @Resource
//    BaseCompanyAreaBService baseCompanyAreaBService;
//
//    @Override
//    @ReadOnlyConnection
//    @Cacheable(key = "'dubbo:list:comp:id:'+#p0", condition = "#companyId != null", cacheNames = "redis1h")
//    public ResponseDTO<CompanyDRO> getCompanyByCompanyId(Integer companyId) {
//        BaseCompanyVO company = companyDao.findByKey(companyId);
//        if (Objects.isNull(company)) {
//            return ResponseDTO.fail("公司不存在");
//        }
//
//        return returnDRO(company);
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<CompanyDRO> getCompanyByCompanyIdNoCache(Integer companyId) {
//        return this.getCompanyByCompanyId(companyId);
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<List<CompanyDRO>> listPageByQuery(BaseCompanyDIO dio, Integer staffId) {
//        log.info("BaseCompanyListRemoteServiceImpl.listPageByQuery参数dio：{}，staffId：{}", dio, staffId);
//        try {
//            List<CompanyDRO> result = baseCompanyBService.listPageByQuery(dio, staffId);
//            return ResponseDTO.success(result);
//        } catch (PlatException e) {
//            return ResponseDTO.fromErrorCodeTable(e.getErrorCode());
//        } catch (Exception e) {
//            log.error("系统异常", e);
//            return ResponseDTO.fail(GlobalConsts.FAILD_MSG);
//        }
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<List<VtDTO>> listVtPageByQuery(BaseCompanyDIO dio, Integer staffId) {
//        log.info("BaseCompanyListRemoteServiceImpl.listVtPageByQuery参数dio：{}，staffId：{}", dio, staffId);
//        try {
//            List<VtDTO> result = baseCompanyBService.listVtPageByQuery(dio, staffId);
//            return ResponseDTO.success(result);
//        } catch (PlatException e) {
//            return ResponseDTO.fromErrorCodeTable(e.getErrorCode());
//        } catch (Exception e) {
//            log.error("系统异常", e);
//            return ResponseDTO.fail(GlobalConsts.FAILD_MSG);
//        }
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<List<VtDTO>> listVtPageByQueryAndLimitPermit(BaseCompanyDIO dio, Boolean limitPermit) {
//        Integer staffId = null;
//        if (limitPermit) {
//            staffId = GUserHolder.getCurrentLoginStaffId();
//            if (NumberUtil.isNullOrZero(staffId)) {
//                staffId = -1;
//            }
//        }
//
//        // -1表示要求数据权限但未通过身份校验
//        if (NumberUtil.isNotNullOrZero(staffId) && staffId == -1) {
//            return ResponseDTO.fail("身份校验失败");
//        }
//
//        return this.listVtPageByQuery(dio, staffId);
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<BasePageDRO<VtDTO>> listVtBOPageByQueryAndLimitPermit(BaseCompanyDIO dio, Boolean limitPermit) {
//        ResponseDTO<List<VtDTO>> responseDTO = this.listVtPageByQueryAndLimitPermit(dio, limitPermit);
//        if (responseDTO.isSuccess()) {
//            BasePageDRO<VtDTO> basePageDRO = new BasePageDRO<>();
//            basePageDRO.setItems(responseDTO.getData());
//            basePageDRO.setPageIndex(dio.getPageIndex());
//            basePageDRO.setPageSize(dio.getPageSize());
//            basePageDRO.setTotalCount(dio.getTotalCount());
//            return ResponseDTO.success(basePageDRO);
//        }
//        ResponseDTO responseError = new ResponseDTO();
//        BeanCopyUtils.copyProperties(responseDTO, responseError);
//        return responseError;
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<CompanyDRO> getCompanyByName(String name) {
//        BaseCompany company = companyDao.findByName(name);
//
//        return returnDRO(company);
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<CompanyDRO> getCompanyByFullName(String fullName) {
//        BaseCompany company = companyDao.findByFullName(fullName);
//
//        return returnDRO(company);
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<List<CompanyDRO>> listCompanyByCompanyNames(List<String> names) {
//        if (CollectionUtils.isEmpty(names)) {
//            return ResponseDTO.fail("参数不能为空");
//        }
//
//        List<BaseCompany> companyList = companyDao.listCompanyByNameList(names);
//
//        List<CompanyDRO> dros = companyList.stream().map(baseCompany -> {
//            CompanyDRO companyDRO = new CompanyDRO();
//            BeanUtils.copyProperties(baseCompany, companyDRO);
//            return companyDRO;
//        }).collect(Collectors.toList());
//
//        return ResponseDTO.success(dros);
//    }
//
//    private ResponseDTO<CompanyDRO> returnDRO(BaseCompany company) {
//        if (Objects.isNull(company)) {
//            return ResponseDTO.success(null, GlobalConsts.SUCCESS_MSG);
//        }
//
//        try {
//            CompanyDRO dro = new CompanyDRO();
//            BeanCopyUtils.copyProperties(company, dro);
//            return ResponseDTO.success(dro, GlobalConsts.SUCCESS_MSG);
//
//        } catch (Exception e) {
//            log.error(e.getMessage());
//            return ResponseDTO.fail("拷贝 CompanyDRO 对象出错");
//        }
//    }
//
//    @Override
//    @ReadOnlyConnection
//    @Cacheable(key = "'dubbo:list:company:ids:'+#p0", cacheNames = "redis1h")
//    public ResponseDTO<List<VtDTO>> listVtByCommpanyIds(List<Integer> ids) {
//        if (CollectionUtils.isEmpty(ids)) {
//            return ResponseDTO.fail("没有对应公司信息");
//        }
//        List<BaseCompany> companys = baseCompanyService.findByKeys(ids, null, null, null);
//
//        List<VtDTO> dtos = getCompanyVtDTOS(companys, null, null);
//
//        return ResponseDTO.success(dtos, GlobalConsts.SUCCESS_MSG);
//    }
//
//    @Override
//    @ReadOnlyConnection
//    @Cacheable(key = "'dubbo:list:company:info:ids:'+#p0", cacheNames = "redis1h")
//    public ResponseDTO<List<CompanyDRO>> listInfoByCompanyIds(List<Integer> ids) {
//        BaseCompanyQuery query = new BaseCompanyQuery();
//        query.setCompanyIds(ids);
//
//        List<BaseCompanyVO> companys = baseCompanyService.listVOByQuery(query);
//
//        List<CompanyDRO> dros = new ArrayList<>();
//
//        for (BaseCompanyVO comp : companys) {
//            CompanyDRO dro = getCompanyDRO(comp);
//            dros.add(dro);
//        }
//
//        return ResponseDTO.success(dros, GlobalConsts.SUCCESS_MSG);
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<List<CompanyDRO>> listByCompanyIdsAndStatus(List<Integer> companyIds, Integer status) {
//        BaseCompanyQuery query = new BaseCompanyQuery();
//        query.setCompanyIds(companyIds);
//        query.setStatus(status);
//        List<BaseCompany>  list = baseCompanyService.listByQueryForMcc(query);
//        List<CompanyDRO> companyDROList =  BeanCopyUtils.copyProperties(list,BaseCompany.class,CompanyDRO.class,CompanyDRO::new);
//        return ResponseDTO.success(companyDROList);
//    }
//
//    @Override
//    @ReadOnlyConnection
//    @Cacheable(key = "'dubbo:list:companys:type:'+#p0+':childType:'+#p1+':status:'+#p2+':plat:'+#p3", cacheNames = "redis1h")
//    public ResponseDTO<List<VtDTO>> listCompanyByType(Integer type, Integer childType, Integer status, Integer plat) {
//        log.info("listCompanyByType参数：type：{}，childType：{}，status：{}，plat：{}", type, childType, status, plat);
//        List<VtDTO> items;
//        try {
//            if (Objects.isNull(status)) {
//                items = companyCache.searchCompanyListByType(type, childType, plat);
//            } else {
//                items = companyCache.searchCompanyListByType(type, childType, status, plat);
//            }
//        } catch (Exception e) {
//            log.error(e.getMessage());
//            return ResponseDTO.fail(e.getMessage());
//        }
//
//        return ResponseDTO.success(items, GlobalConsts.SUCCESS_MSG);
//    }
//
//    @Override
//    @ReadOnlyConnection
//    @Cacheable(key = "'dubbo:list:comp:vt:pagenum:'+#p0+':pagesize:'+#p1", cacheNames = "redis1h")
//    public ResponseDTO<List<VtDTO>> listCompanyVtByPage(Integer pageNum, Integer pageSize) {
//        log.info("listCompanyVtByPage参数：pageNum：{}，pageSize：{}", pageNum, pageSize);
//        try {
//            List<VtDTO> result = baseCompanyBService.listCompanyVtByPage(pageNum, pageSize);
//            return ResponseDTO.success(result);
//        } catch (PlatException e) {
//            return ResponseDTO.fromErrorCodeTable(e.getErrorCode());
//        } catch (Exception e) {
//            log.error("listCompanyVtByPage系统异常", e);
//            return ResponseDTO.fail(GlobalConsts.FAILD_MSG);
//        }
//    }
//
//    @Override
//    @ReadOnlyConnection
//    @Cacheable(key = "'dubbo:list:comp:info:pagenum:'+#p0+':pagesize:'+#p1", cacheNames = "redis1h")
//    public ResponseDTO<List<CompanyDRO>> listAllCompanyInfoByPage(Integer pageNum, Integer pageSize) {
//        log.info("listAllCompanyInfoByPage参数：pageNum：{}，pageSize：{}", pageNum, pageSize);
//        try {
//            List<CompanyDRO> result = baseCompanyBService.listAllCompanyInfoByPage(pageNum, pageSize);
//            return ResponseDTO.success(result);
//        } catch (PlatException e) {
//            return ResponseDTO.fromErrorCodeTable(e.getErrorCode());
//        } catch (Exception e) {
//            log.error("listAllCompanyInfoByPage系统异常", e);
//            return ResponseDTO.fail(GlobalConsts.FAILD_MSG);
//        }
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<List<CompanyDRO>> listCompanyByOrgIds(List<Integer> orgIds) {
//        log.info("BaseCompanyListRemoteServiceImpl.listCompanyByOrgIds参数orgIds：{}", orgIds);
//        if (CollectionUtils.isEmpty(orgIds)) {
//            return ResponseDTO.fail("组织架构ID不能为空");
//        }
//
//        if (orgIds.size() > 100) {
//            return ResponseDTO.fail("组织架构ID集合最大长度不能超过100");
//        }
//
//        List<BaseCompany> baseCompanyList = baseCompanyService.listByOrganIds(orgIds);
//
//        List<CompanyDRO> dros = baseCompanyList.stream().map(baseCompany -> {
//            CompanyDRO companyDRO = new CompanyDRO();
//            BeanUtils.copyProperties(baseCompany, companyDRO);
//            return companyDRO;
//        }).collect(Collectors.toList());
//
//        return ResponseDTO.success(dros);
//    }
//
//    @Override
//    @ReadOnlyConnection
//    @Cacheable(key = "'dubbo:list:comp:ZmnAllServiceCompany:'", cacheNames = "redis1d")
//    public ResponseDTO<List<CompanyDRO>> listZmnAllInfomationCompany() {
//        try {
//            List<CompanyDRO> result = baseCompanyBService.listZmnAllInfomationCompany();
//            return ResponseDTO.success(result);
//        } catch (Exception e) {
//            log.error("listZmnAllInfomationCompany系统异常", e);
//            return ResponseDTO.fail(GlobalConsts.FAILD_MSG);
//        }
//    }
//
//    @Override
//    @ReadOnlyConnection
//    @Cacheable(key = "'dubbo:list:comp:ZmnAllElectricCompany:'", cacheNames = "redis1d")
//    public ResponseDTO<List<CompanyDRO>> listZmnAllElectricCompany() {
//        try {
//            List<CompanyDRO> result = baseCompanyBService.listZmnAllElectricCompany();
//            return ResponseDTO.success(result);
//        } catch (Exception e) {
//            log.error("listZmnAllElectricCompany系统异常", e);
//            return ResponseDTO.fail(GlobalConsts.FAILD_MSG);
//        }
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<List<CompanyTargetDRO>> listCompanyTarget(Integer companyId, Integer cityId) {
//        log.info("BaseCompanyListRemoteServiceImpl.listCompanyTarget参数companyId：{}，cityId：{}", companyId, cityId);
//        List<BaseCompanyTarget> list = baseCompanyService.listCompanyTarget(companyId, cityId);
//
//        List<CompanyTargetDRO> dros = new ArrayList<>();
//        list.forEach(target -> {
//            CompanyTargetDRO dro = new CompanyTargetDRO();
//            dro.setCityId(target.getCityId());
//            dro.setCityName(target.getCityName());
//            dro.setCompanyId(target.getCompanyId());
//            dro.setCompanyName(target.getCompanyName());
//            dro.setIncome(target.getIncome());
//            dro.setOrderQuantityCompleted(target.getOrderQuantityCompleted());
//            dro.setYear(target.getYear());
//            dros.add(dro);
//        });
//
//        return ResponseDTO.success(dros, GlobalConsts.SUCCESS_MSG);
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<List<CompanyDRO>> listCompanyByPlatAndType(Integer plat, Integer type, Integer childType) {
//        log.info("BaseCompanyListRemoteServiceImpl.listCompanyByPlatAndType参数plat：{}，type：{}，childType：{}", plat, type, childType);
//
//        BaseCompanyQuery query = new BaseCompanyQuery();
//        query.setPlat(plat);
//        query.setType(type);
//
//        try {
//            List<CompanyDRO> collect = Optional.ofNullable(baseCompanyService.listByQuery(query)).orElse(Collections.emptyList()).stream().map(mm -> {
//                CompanyDRO companyDro = new CompanyDRO();
//                BeanUtils.copyProperties(mm, companyDro);
//                return companyDro;
//            }).collect(Collectors.toList());
//            return ResponseDTO.success(collect, "获取数据成功!");
//        } catch (Exception e) {
//            log.error("获取数据失败, exception:{}, msg:{}", e, e.getMessage());
//            return ResponseDTO.fail("获取数据失败");
//        }
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<List<CompanyDRO>> listCompanyByTypes(List<CompanyTypeDIO> companyTypeDIOList, Integer plat) {
//        log.info("BaseCompanyListRemoteServiceImpl.listCompanyByTypes参数companyTypeDIOList：{}，plat：{}", companyTypeDIOList, plat);
//        try {
//            List<CompanyTypeQuery> types =
//                companyTypeDIOList.stream().map(companyTypeDIO -> new CompanyTypeQuery(companyTypeDIO.getType(), companyTypeDIO.getChildType())).collect(Collectors.toList());
//
//            BaseCompanyQuery query = new BaseCompanyQuery();
//            query.setPlat(plat);
//            query.setCompanyTypeQueryList(types);
//            query.setStatus(GlobalConsts.YES);
//
//            List<CompanyDRO> collect = Optional.ofNullable(baseCompanyService.listVOByQuery(query)).orElse(Collections.emptyList()).stream().map(mm -> {
//                CompanyDRO companyDro = new CompanyDRO();
//                BeanUtils.copyProperties(mm, companyDro);
//                return companyDro;
//            }).collect(Collectors.toList());
//            return ResponseDTO.success(collect, "获取数据成功!");
//        } catch (Exception e) {
//            log.error("获取数据失败, exception:{}, msg:{}", e, e.getMessage());
//            return ResponseDTO.fail("获取数据失败");
//        }
//    }
//
//    /**
//     * 描述: 根据类型，平台标志查询公司信息 添加上上个月修改为禁用的数据
//     *
//     * @param types
//     *            类型：1服务商，2厂家，3供应商，4大客户，5平台(直接子公司)，6渠道，7信息公司，8电器公司
//     * @param plat
//     *            平台标志：1.言而有信 2.啄木鸟,3川南环保
//     * @return com.zmn.common.dto.ResponseDTO<com.zmn.plat.dubbo.dto.company.CompanyDRO>
//     * @author wangjie
//     * @since 17:22 2019/7/11
//     **/
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<List<CompanyDRO>> listCompanyByTypesUpdate(List<CompanyTypeDIO> types, Integer plat) {
//        log.info("BaseCompanyListRemoteServiceImpl.listCompanyByTypesUpdate参数types：{}，plat：{}", types, plat);
//        return this.listCompanyByTypesUpdate(types, plat, 2);
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<List<CompanyDRO>> listCompanyByTypesUpdate(List<CompanyTypeDIO> types, Integer plat, Integer month) {
//        if (CollectionUtils.isEmpty(types)) {
//            return ResponseDTO.fail("公司类型不能为空");
//        }
//
//        List<CompanyTypeQuery> companyTypeQueryList = types.stream().map(companyTypeDIO -> new CompanyTypeQuery(companyTypeDIO.getType(), companyTypeDIO.getChildType())).collect(Collectors.toList());
//        log.info("BaseCompanyListRemoteServiceImpl.listCompanyByTypesUpdate参数types：{}，plat：{}，month：{}", types, plat, month);
//
//        try {
//            List<CompanyDRO> result = baseCompanyBService.listCompanyByTypesUpdate(types, plat, month);
//            return ResponseDTO.success(result);
//        } catch (PlatException e) {
//            return ResponseDTO.fromErrorCodeTable(e.getErrorCode());
//        } catch (Exception e) {
//            log.error("listCompanyByTypesUpdate系统异常", e);
//            return ResponseDTO.fail(GlobalConsts.FAILD_MSG);
//        }
//    }
//
//    @Override
//    @ReadOnlyConnection
//    public ResponseDTO<Integer> countByQuery(BaseCompanyDIO dio, Integer staffId) {
//        log.info("BaseCompanyListRemoteServiceImpl.countByQuery参数dio：{}，staffId：{}", dio, staffId);
//
//        BaseCompanyQuery query = new BaseCompanyQuery();
//        BeanCopyUtils.copyProperties(dio, query);
//
//        List<CompanyTypeDIO> companyTypeQueryList = dio.getCompanyTypeQueryList();
//        if (!CollectionUtils.isEmpty(companyTypeQueryList)) {
//            List<CompanyTypeQuery> queryList = companyTypeQueryList.stream().map(companyTypeDIO -> {
//                CompanyTypeQuery typeQuery = new CompanyTypeQuery();
//                BeanUtils.copyProperties(companyTypeDIO, typeQuery);
//                return typeQuery;
//            }).collect(Collectors.toList());
//            query.setCompanyTypeQueryList(queryList);
//        }
//
//        if (NumberUtil.isPositiveInteger(staffId)) {
//            query.setOperatorId(staffId);
//        }
//
//        Integer count = baseCompanyService.countByQuery(query);
//        return ResponseDTO.success(count);
//    }
//
//    @NotNull
//    private CompanyDRO getCompanyDRO(BaseCompanyVO company) {
//        CompanyDRO dro = new CompanyDRO();
//        dro.setCompanyId(company.getCompanyId());
//        dro.setName(company.getName());
//        dro.setSubCompanyId(company.getSubCompanyId());
//        dro.setSubCompanyName(company.getSubCompanyName());
//        dro.setFullName(company.getFullName());
//        dro.setType(company.getType());
//        dro.setFirstLetter(company.getFirstLetter());
//        dro.setProvinceId(company.getProvinceId());
//        dro.setCityId(company.getCityId());
//        dro.setAddress(company.getAddress());
//        dro.setBrief(company.getBrief());
//        dro.setBusinessLicense(company.getBusinessLicense());
//        dro.setContact(company.getContact());
//        dro.setMobile(company.getMobile());
//        dro.setStatus(company.getStatus() == null ? GlobalConsts.NO : company.getStatus());
//        dro.setStreet(company.getStreet());
//        dro.setProvinceName(company.getProvinceName());
//        dro.setCityName(company.getCityName());
//        dro.setPlat(company.getPlat());
//        dro.setCountyName(company.getCountyName());
//        dro.setPlat(company.getPlat());
//        dro.setOrgId(company.getOrgId());
//        dro.setOrgName(company.getOrgName());
//        dro.setOrgTwoId(company.getOrgTwoId());
//        dro.setOrgTwoName(company.getOrgTwoName());
//        dro.setOrgThrId(company.getOrgThrId());
//        dro.setOrgThrName(company.getOrgThrName());
//        return dro;
//    }
//
//    @NotNull
//    private List<VtDTO> getCompanyVtDTOS(List<BaseCompany> companys, Integer type, Integer status) {
//        List<VtDTO> dtos = new ArrayList<>();
//        for (BaseCompany company : companys) {
//            if (type != null && !company.getType().equals(type)) {
//                continue;
//            }
//
//            if (status != null && !company.getStatus().equals(status)) {
//                continue;
//            }
//
//            dtos.add(new VtDTO(company.getCompanyId(), company.getName(), company.getStatus()));
//        }
//        return dtos;
//    }
//}
